package com.lance.answer.gui.answer.gk.answer.lms.batch;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.lance.answer.gui.answer.AnswerObject;
import com.lance.answer.gui.answer.DealJsonString;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.gui.answer.gk.answer.lms.batch.dto.LmsWatchDTO;
import com.lance.answer.gui.answer.gk.answer.lms.batch.task.DoAnswerTask;
import com.lance.answer.gui.answer.gk.answer.lms.batch.task.DoAnswerTasktmp;
import com.lance.answer.gui.answer.gk.answer.lms.util.LmsUtil;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.LinkedBlockingDeque;

public class DoAnswertmpppppppp {


    public static void main(String[] args) throws Exception {

//        String subjectName= "土木工程力学（本）";
//        File file = new File("D:\\project\\answer-tool\\src\\main\\resources\\" + subjectName + "\\" + subjectName + "-answer.json");
//        System.out.println(file.exists());
//        InputStream inputStream = new FileInputStream(file);
//        String answerStr = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
//        List<AnswerObject> list = JSONObject.parseArray(answerStr, AnswerObject.class);
//        System.err.println(list);
//        Thread.sleep(1000000);


        int sync = 1;

        LinkedBlockingDeque<LmsWatchDTO> watchQueue = new LinkedBlockingDeque<>();

        InputStream inputStream2 = new FileInputStream("libs//2022秋//fengx.user");
        List<String> userString = IOUtils.readLines(inputStream2, "utf-8");

        for (String user : userString) {
            String[] strs = user.split(",");
            String userCode = strs[0];

            if(!"2211001201588".equals(userCode)){
                continue;
            }

            String pwd = strs[1];
            LmsWatchDTO lmsWatchDTO = new LmsWatchDTO();
            lmsWatchDTO.setUsername(userCode);
            lmsWatchDTO.setPassword(pwd);

            Map<Long, String> courseMap = LmsUtil.getUserCourseMap(userCode);

            Map<Long, Map<String, AnswerObject>> answers= Maps.newHashMap();
            for(Long courseId:courseMap.keySet()){
                Map<String, AnswerObject> answerMap = initAnswer(courseMap.get(courseId));
                answers.put(courseId,answerMap);
            }
            lmsWatchDTO.setCourseMap(courseMap);
            lmsWatchDTO.setAnswers(answers);
            watchQueue.add(lmsWatchDTO);

        }

        for (int i = 1; i <= sync; i++) {
            Thread answerThread = new DoAnswerTasktmp("d:\\ynouAutoStudy\\chromedriver.exe", watchQueue);
            //log.info("加载课程ID：{} 名称：{} ", openCourseId,subjectName);
            answerThread.setName("答题线程--" + i);
            answerThread.start();
        }
    }




    private static Map<String, AnswerObject> initAnswer(String subjectName) throws IOException {
   //     if(!subjectName.contains("形势与政策")){

      //  if(!subjectName.contains("中国")&&!subjectName.contains("形势与政策")){
        if(!subjectName.contains("中国近现代史纲要")){
            return null;
        }

        System.err.println(">>>>>>>>>>:"+subjectName);// 企业现场质量管理


        subjectName = subjectName.replaceAll("#","");
        subjectName = subjectName.replaceAll("＃","");
        subjectName = subjectName.trim();
        File file = new File("D:\\project\\answer-tool\\src\\main\\resources\\" + subjectName + "\\" + subjectName + "-answer.json");
        if(!file.exists()){
            System.err.println("不存在。。。。。。。。。");
            return null;
        }
        InputStream inputStream = new FileInputStream(file);
        String answerStr = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        List<AnswerObject> list = JSONObject.parseArray(answerStr, AnswerObject.class);
        Map<String, AnswerObject> answerMap = Maps.newHashMap();

        boolean isEngCourse;
        isEngCourse = subjectName.contains("英语");
        for (AnswerObject answerObject : list) {
            answerMap.put(answerObject.getQuestionTitle(), answerObject);
            if (isEngCourse) {
                answerMap.put(DealJsonString.removeSpace(answerObject.getQuestionTitle()), answerObject);
            } else {
                answerMap.put(DealJsonString.saveZhWord(answerObject.getQuestionTitle()), answerObject);
                answerMap.put(Util.dealContent(answerObject.getQuestionTitle()), answerObject);
                answerMap.put(DealJsonString.delHTMLTag(Util.dealContent(answerObject.getQuestionTitle())), answerObject);
            }
            if(subjectName.contains("微积分")){
               // originAnswers.put(dealTitleWithHref(title),answerMap.get(title));
                answerMap.put(DoAnswerTask.dealTitleWithHref(answerObject.getQuestionTitle()), answerObject);
            }
        }

        return answerMap;
    }

}
